<?php
/**
 * insert row to table detil_prestasi_pelanggaran
 * @param $key
 * @return result
 */
function deprepel_select($key=false, $orderby=false, $groupby=false){
	$sql = "select * from detil_prestasi_pelanggaran";
	if ($key){
		$sql .= " where 
				DETACHIEV_ID LIKE '%$key%' OR
				DETACHIEV_NAMA LIKE '%$key%' OR 
				DETACHIEV_TINGKAT LIKE '%$key%' OR  
				ACHIEV_ID LIKE '%$key%'";
	}
	if ($groupby){ $sql .= " GROUP BY $groupby"; }
	if ($orderby){ $sql .= " ORDER BY $orderby"; }

	$result = mysql_query($sql);
	return !$result? mysql_error() : $result;
}

function deprepel_select_corelate($key=false, $where=false, $orderby=false, $groupby=false){
	$sql = "select * from prestasipelanggaran as a, detil_prestasi_pelanggaran as b ";
	$sql .= " where a.ACHIEV_ID = b.ACHIEV_ID ";
	if ($key){
		$sql .= " AND (
				b.DETACHIEV_ID LIKE '%$key%' OR
				b.DETACHIEV_NAMA LIKE '%$key%' OR 
				b.DETACHIEV_TINGKAT LIKE '%$key%' OR  
				a.ACHIEV_ID LIKE '%$key%' ) ";
	}
	if ($where) $sql .= " AND $where ";
	if ($groupby) $sql .= " GROUP BY $groupby ";
	if ($orderby) $sql .= " ORDER BY $orderby ";
	$result = mysql_query($sql);
	return !$result? mysql_error() : $result;
}

function deprepel_generate_id(){
	//$sql = "SELECT count(*) from detil_prestasi_pelanggaran WHERE 1";
	//$result = mysql_query($sql);
	//$result = mysql_fetch_assoc($result);
	//return 'R'.($result['max']+1);
//return false;
}

/**
 * load row from table detil_prestasi_pelanggaran
 * @param $id
 * @return result
 */
function deprepel_load($id){
	$sql = "select * from detil_prestasi_pelanggaran
			where DETACHIEV_ID = '$id'";
	return mysql_query($sql);
}

/**
 * check wheter the specific id is exist on the table detil_prestasi_pelanggaran
 * @param $id specific id
 * @return boolean value (true:exist, false:otherwise)
 */
function deprepel_isExistID($id){
	$sql = "select * from detil_prestasi_pelanggaran
			where DETACHIEV_ID = '$id'";
	
	$result = mysql_query($sql);
	$result = mysql_fetch_assoc($result);
	if ($res['DETACHIEV_ID'] === $id) return true;
	return false;
}

/**
 * insert a record to detil_prestasi_pelanggaran table
 * @param $DETACHIEV_ID
 * @param $DETACHIEV_NAMA
 * @param $DETACHIEV_TINGKAT
 * @param $DETACHIEV_MIN
 * @param $DETACHIEV_MAX
 * @param $ACHIEV_ID
 * @return unknown_type
 */
function deprepel_add($DETACHIEV_ID, $DETACHIEV_NAMA, $DETACHIEV_TINGKAT, $ACHIEV_ID){
	$sql = "insert into detil_prestasi_pelanggaran ".
			"values ('$DETACHIEV_ID', '$ACHIEV_ID', '$DETACHIEV_NAMA', '$DETACHIEV_TINGKAT')";
	return mysql_query($sql);
}

/**
 * update a row in detil_prestasi_pelanggaran table
 * @param $DETACHIEV_ID
 * @param $DETACHIEV_NAMA
 * @param $DETACHIEV_TINGKAT
 * @param $DETACHIEV_MIN
 * @param $DETACHIEV_MAX
 * @param $ACHIEV_ID
 * @return unknown_type
 */
function deprepel_update($DETACHIEV_ID, $DETACHIEV_NAMA, $DETACHIEV_TINGKAT, $ACHIEV_ID){
	$sql = "update detil_prestasi_pelanggaran ".
			"set DETACHIEV_NAMA = '$DETACHIEV_NAMA',
				DETACHIEV_TINGKAT = '$DETACHIEV_TINGKAT',
				ACHIEV_ID = '$ACHIEV_ID' 
			where DETACHIEV_ID ='$DETACHIEV_ID'";
	return mysql_query($sql);
}

/**
 * delete a row in detil_prestasi_pelanggaran table
 * @param $DETACHIEV_ID
 * @return boolean
 */
function deprepel_delete($DETACHIEV_ID){
	$sql = "delete from detil_prestasi_pelanggaran ".
			"where DETACHIEV_ID='$DETACHIEV_ID'";
	return mysql_query($sql);
}